﻿#include"InventoryModel.h"

InventoryModel::InventoryModel()
{
}

InventoryModel::InventoryModel(const QVariantMap& data, bool base64)
{
	setData(data,base64);
}

InventoryModel::~InventoryModel()
{
}

QStringList InventoryModel::fields() const
{
    return 
    {
        "id","name","type","currentInventory","safetyInventory","restockingQuantity","mtime","ctime"
    };
}

QVariantMap InventoryModel::toMap(const QStringList& fields, bool base64) const
{
	auto comparison = this->fields();
	int nSel = 0;
	QVariantMap map;
	if (fields.contains(comparison[nSel]))
		map[comparison[nSel++]] = m_id;
	if (fields.contains(comparison[nSel]))
		map[comparison[nSel++]] = m_name;
	if (fields.contains(comparison[nSel]))
		map[comparison[nSel++]] = m_type;
	if (fields.contains(comparison[nSel]))
		map[comparison[nSel++]] = m_currentInventory;
	if (fields.contains(comparison[nSel]))
		map[comparison[nSel++]] = m_safetyInventory;
	if (fields.contains(comparison[nSel]))
		map[comparison[nSel++]] = m_restockingQuantity;
	if (fields.contains(comparison[nSel]))
		map[comparison[nSel++]] = m_mtime;
	if (fields.contains(comparison[nSel]))
		map[comparison[nSel++]] = m_ctime;
	return map;
}

void InventoryModel::setData(const QVariantMap& data, bool base64)
{
	auto comparison = this->fields();
	int nSel = 0;
	if (data.contains(comparison[nSel]))
		m_id = data[comparison[nSel++]].toULongLong();
	if (data.contains(comparison[nSel]))
		m_name = data[comparison[nSel++]].toString();
	if (data.contains(comparison[nSel]))
		m_type = data[comparison[nSel++]].toString();
	if (data.contains(comparison[nSel]))
		m_currentInventory = data[comparison[nSel++]].toULongLong();
	if (data.contains(comparison[nSel]))
		m_safetyInventory = data[comparison[nSel++]].toULongLong();
	if (data.contains(comparison[nSel]))
		m_restockingQuantity = data[comparison[nSel++]].toULongLong();
	if (data.contains(comparison[nSel]))
		m_mtime = data[comparison[nSel++]].toDateTime();
	if (data.contains(comparison[nSel]))
		m_ctime = data[comparison[nSel++]].toDateTime();
	
}

QString InventoryModel::createTableSql(const QString& tableName) const
{
	return QString("CREATE TABLE `%1` ("
		"id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,"//id
		"name VARCHAR(50),"//名字
		"type VARCHAR(50),"//类型
		"currentInventory BIGINT UNSIGNED,"//当前库存
		"safetyInventory BIGINT UNSIGNED,"//安全库存
		"restockingQuantity BIGINT UNSIGNED,"//补货量
		"mtime DATETIME ,"//修改时间
		"ctime DATETIME "//创建时间
		")").arg(tableName);
}

quint64 InventoryModel::inventory_id() const
{
	return m_id;
}

QString InventoryModel::name() const
{
	return m_name;
}

QString InventoryModel::type() const
{
	return m_type;
}

quint64 InventoryModel::currentInventory() const
{
	return m_currentInventory;
}

quint64 InventoryModel::safetyInventory() const
{
	return m_safetyInventory;
}

quint64 InventoryModel::restockingQuantity() const
{
	return m_restockingQuantity;
}

QDateTime InventoryModel::ctime() const
{
	return m_ctime;
}

QDateTime InventoryModel::mtime() const
{
	return m_mtime;
}

void InventoryModel::setInventory_id(quint64 id)
{
	m_id = id;
	setMTime();
}

void InventoryModel::setName(const QString& name)
{
	m_name = name;
	setMTime();
}

void InventoryModel::setType(const QString& type)
{
	m_type = type;
	setMTime();
}

void InventoryModel::setCurrentInventory(quint64 currentInventory)
{
	m_currentInventory = currentInventory;
	setMTime();
}

void InventoryModel::setSafetyInventory(quint64 safetyInventory)
{
	m_safetyInventory = safetyInventory;
	setMTime();
}

void InventoryModel::setRestockingQuantity(quint64 restockingQuantity)
{
	m_restockingQuantity = restockingQuantity;
	setMTime();
}

void InventoryModel::setCTime(const QDateTime& time)
{
	m_ctime = time;
}

void InventoryModel::setMTime(const QDateTime& time)
{
	m_mtime = time;
}
